# 添加右键菜单的PowerShell脚本
# 该代码已弃用 留作参考
$softwarePath = "C:\Program Files\XX软件\xx.exe"
$menuName = "用XX软件打开"

function Add-FileRightClickMenu {
    # 为所有文件添加
    $keyPath = "HKCU:\Software\Classes\*\shell\$menuName"
    New-Item -Path $keyPath -Force | Out-Null
    Set-ItemProperty -Path $keyPath -Name "Icon" -Value $softwarePath
    
    $commandPath = "$keyPath\command"
    New-Item -Path $commandPath -Force | Out-Null
    Set-ItemProperty -Path $commandPath -Name "(Default)" -Value "`"$softwarePath`" `"%1`""
}

function Add-FolderRightClickMenu {
    # 为文件夹添加
    $keyPath = "HKCU:\Software\Classes\Directory\shell\$menuName"
    New-Item -Path $keyPath -Force | Out-Null
    Set-ItemProperty -Path $keyPath -Name "Icon" -Value $softwarePath
    
    $commandPath = "$keyPath\command"
    New-Item -Path $commandPath -Force | Out-Null
    Set-ItemProperty -Path $commandPath -Name "(Default)" -Value "`"$softwarePath`" `"%1`""
}

function Add-SpecificFileType {
    param([string]$fileExtension)
    
    # 为特定文件类型添加
    $keyPath = "HKCU:\Software\Classes\$fileExtension\shell\$menuName"
    New-Item -Path $keyPath -Force | Out-Null
    Set-ItemProperty -Path $keyPath -Name "Icon" -Value $softwarePath
    
    $commandPath = "$keyPath\command"
    New-Item -Path $commandPath -Force | Out-Null
    Set-ItemProperty -Path $commandPath -Name "(Default)" -Value "`"$softwarePath`" `"%1`""
}

# 执行添加
Write-Host "正在添加右键菜单..." -ForegroundColor Green

Add-FileRightClickMenu
Add-FolderRightClickMenu

# 为常见文件类型添加
$commonExtensions = @(".txt", ".pdf", ".doc", ".docx", ".jpg", ".png")
foreach ($ext in $commonExtensions) {
    Add-SpecificFileType -fileExtension $ext
}

Write-Host "右键菜单添加完成！" -ForegroundColor Green
Write-Host "可能需要重启文件资源管理器或重新登录才能生效。" -ForegroundColor Yellow